<?php
/**
 * author:歪妻
 * Date: 2020/11/6 0006
 */

/**
 * @return int|mixed
 * 接收index.php传过来的id
 */
function postId()
{

    if (isset($_GET['post_id'])){
        $postId = $_GET['post_id'];
    }else{
        //总是定义一个id
        $postId = 0;
    }
    return $postId;

}

function getView(PDO $pdo,$postId)
{
    $stmt = $pdo->prepare(
        'SELECT
        title, created_at, body
    FROM
        post
    WHERE
        id = :id'
    );
    if ($stmt === false)
    {
        throw new Exception('准备此查询时出现问题');
    }
    $result = $stmt->execute(
        array( 'id'=> $postId,)
    );
    if ($result === false)
    {
        throw new Exception('运行此查询时出现问题');
    }
    return $stmt->fetch(PDO::FETCH_ASSOC);
}
